package org.micode.demo.web.service;

import org.micode.common.domain.DataPage;
import org.micode.common.domain.vo.EditObjVO;
import org.micode.common.domain.vo.KeyVO;
import org.micode.common.enums.ResultEnum;
import org.micode.common.exception.DataException;
import org.micode.common.helper.I18nMsgHelper;
import org.micode.common.utils.BeanExtUtils;
import org.micode.common.utils.ObjectUtils;
import org.micode.demo.biz.service.BizSysUserService;
import org.micode.demo.db.domain.dto.SysUserDTO;
import org.micode.demo.db.domain.dto.SysUserExtDTO;
import org.micode.demo.db.domain.query.SysUserQuery;
import org.micode.demo.db.service.DaoSysUserExtService;
import org.micode.demo.db.service.DaoSysUserRoleService;
import org.micode.demo.db.service.DaoSysUserService;
import org.micode.demo.db.vo.ChangePwdVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户信息表(sys_user) APP服务实现类
 * <p>
 * author: wutianbin
 */
@Service
@Slf4j
public class SysUserService {

    @Autowired
    private BizSysUserService bizSysUserService;

    @Autowired
    private DaoSysUserService daoSysUserService;

    @Autowired
    private DaoSysUserRoleService daoSysUserRoleService;

    @Autowired
    private DaoSysUserExtService daoSysUserExtService;

    public DataPage<SysUserDTO> list(SysUserQuery dataQuery) {
        DataPage<SysUserDTO> dataPage = daoSysUserService.listPage(dataQuery);
        if (null != dataPage && null != dataPage.getList()) {
            dataPage.getList().forEach(item -> {
                item.setPassword(null);
                item.setSalt(null);
            });
        }
        return dataPage;
    }

    public SysUserDTO info(KeyVO<Long> userId) {
        SysUserDTO user = daoSysUserService.get(userId.getKey());
        if (null != user) {
            user.setPassword(null);
            user.setSalt(null);
        }
        return user;
    }

    public SysUserDTO insert(SysUserDTO sysUser) {
        // 调用管理服务插入新记录，管理服务中要处理用户密码
        return bizSysUserService.insert(sysUser);
    }

    public Integer update(SysUserDTO sysUser) {
        // 修改信息时不能修改密码
        sysUser.setConfirmPassword(null);
        sysUser.setPassword(null);

        // 直接调用数据中心修改信息
        return daoSysUserService.update(sysUser);
    }

    public Integer delete(EditObjVO<List<Long>> delIds) {
        // 默认用户不能删除，所以需要先排除默认用户
        List<Long> ids = daoSysUserService.listNotDefault(delIds.getWantEdit());
        if (null == ids || ids.size() <= 0) {
            return 0;
        } else if (ids.size() < delIds.getWantEdit().size()) {
            // 选择的用户里面有系统规默认用户
            throw new DataException(I18nMsgHelper.get("valid.user.delete.have-default-user"));
        }

        return daoSysUserService.deleteByKeys(delIds);
    }

    public Integer resetPassword(SysUserDTO sysUser) {
        return bizSysUserService.resetPassword(sysUser);
    }

    public Integer resetLoginNum(SysUserDTO sysUser) {
        SysUserExtDTO userExtDTO = daoSysUserExtService.get(sysUser.getUserId());
        if (null == userExtDTO) {
            userExtDTO = new SysUserExtDTO();
            BeanExtUtils.copyProperties(sysUser, userExtDTO);
            daoSysUserExtService.insert(userExtDTO);
            return 1;
        } else {
            userExtDTO.setLoginNum(0L);
            return daoSysUserExtService.update(userExtDTO);
        }
    }

}

